/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package montecarlo;

import static java.lang.Math.exp;
import static java.lang.Math.max;
import static java.lang.Math.sqrt;
import java.util.ArrayList;
import java.util.Random;
import java.util.concurrent.Callable;

/**
 *
 * @author Valentin
 */
public class MonteCarloFonctions{
    
    
    String CallPutFlag="c";
    Double S=40.0;
    Double X=50.0;
    Double T=0.5;
    Double r=0.06;
    Double b=0.10;
    Double v=0.45;
    int nSteps=168;
    int nSimulations=100000; 
    Double resultat=0.0;
    Long t0, t1;
    ArrayList<Double> listeResultats = new ArrayList<Double>();
    
    public MonteCarloFonctions()
    {
        
    }

    
    public void MonteCarloStandardOption()
    {
        Double dt, St, Sum=0.0, Drift, vSqrdt;
        Double MonteCarloStandardOption;
        int i, j, z=0;
        Random rand;
        
        dt = T / nSteps;
        Drift = (b-(v*v)/2)*dt;
        vSqrdt = v*sqrt(dt);
        
        if(CallPutFlag=="c")
        {
            z=1;
        }
        else if(CallPutFlag=="p")
        {
            z=-1;
        }
        
        for(i=0; i<nSimulations; i++)
        {
            St=S;
            
            for(j=0; j<nSteps; j++)
            {
                rand = new Random();
                St = St*exp(Drift+vSqrdt*rand.nextGaussian());
                Sum = Sum+max(z*(St-X),0);   
            }
        }
        
        MonteCarloStandardOption = (exp(T*(-r)))*(Sum/nSimulations);
        System.out.println(MonteCarloStandardOption);     
    }
    
    
    
    
    
    //Calcule une simulation et ajoute le résultat dans une arraylist
    public Double MonteCarloUneSimulation()
    {
        Double dt, St, Sum=0.0, Drift, vSqrdt;
        Double MonteCarloStandardOption;
        int i, j, z=0;
        Random rand;
        
        dt = T / nSteps;
        Drift = (b-(v*v)/2)*dt;
        vSqrdt = v*sqrt(dt);
        
        if(CallPutFlag=="c")
        {
            z=1;
        }
        else if(CallPutFlag=="p")
        {
            z=-1;
        }
        
        
            St=S;
            
            for(j=0; j<nSteps; j++)
            {
                rand = new Random();
                St = St*exp(Drift+vSqrdt*rand.nextGaussian());
                Sum = Sum+max(z*(St-X),0);   
            }
        
        
        MonteCarloStandardOption = (exp(T*(-r)))*(Sum);
        
        listeResultats.add(MonteCarloStandardOption);
        return MonteCarloStandardOption;
        
    }
    
    
    
    
    /*
    //retourne le résultat moyen des différentes simulations stockées dans l'arraylist
    public void MoyenneSimulations()
    {        
        for(int i=0; i<listeResultats.size();i++)
        {
            resultat = resultat+listeResultats.get(i);
        }
        
        resultat = resultat/listeResultats.size();
        System.out.println(resultat);
    }
   */ 

    
    
 /*   @Override
    public void run() {
    
        t0=System.currentTimeMillis();
        for(int i=0; i<nSimulations; i++)
        {
            MonteCarloUneSimulation();
        }
        
        MoyenneSimulations();
        t1=System.currentTimeMillis();
        System.out.println("résultat calculé en "+(t1-t0)+" mS");
    }*/

  
}
